package com.jp.fm.data;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.lang3.builder.ToStringBuilder;

import com.jp.fm.HomeFinanceRunner;
import com.jp.fm.TestHelper;

public class Site implements Serializable {

	private static final String COLUMN_VERSION = "VERSION";
	private static final String COLUMN_DB_ENCRYPTED = "DB_ENCRYPTED";

	private static final long serialVersionUID = 1L;

	private Float version = null;
	private String siteKey = null;
	private boolean dbEncrypted = true;

	public static Site findSite() {

		Site ret = null;

		Connection c = null;
		PreparedStatement p = null;
		ResultSet rs = null;

		try {
			c = HomeFinanceRunner.getConnection();

			p = c.prepareStatement("select version, db_encrypted from site");

			rs = p.executeQuery();

			if (rs.next()) {
				ret = new Site(rs.getFloat(COLUMN_VERSION),
						HomeFinanceRunner.convertIntToBool(rs
								.getInt(COLUMN_DB_ENCRYPTED)));

				// Get the site key
				InputStream inStream = TestHelper.class
						.getResourceAsStream("/hfm.properties");
				Properties props = new Properties();
				props.load(inStream);
				ret.setSiteKey(props.getProperty("key"));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			HomeFinanceRunner.closeConnection(c, p, rs);
		}

		return ret;
	}

	public void writeObject() {
		Connection c = null;
		PreparedStatement p = null;
		ResultSet rs = null;

		try {
			c = HomeFinanceRunner.getConnection();

			p = c.prepareStatement("update site set version = ?, db_encrypted = ?");
			p.setFloat(1, this.getVersion());
			p.setInt(2,
					HomeFinanceRunner.convertBoolToInt(this.isDbEncrypted()));
			p.executeUpdate();
			c.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			HomeFinanceRunner.closeConnection(c, p, rs);
		}
	}

	public Site(final Float version, final boolean dbEncrypted) {
		super();
		this.version = version;
		this.dbEncrypted = dbEncrypted;
	}

	public final Float getVersion() {
		return version;
	}

	public final boolean isDbEncrypted() {
		return dbEncrypted;
	}

	public final void setVersion(final Float version) {
		this.version = version;
	}

	public final void setDbEncrypted(final boolean dbEncrypted) {
		this.dbEncrypted = dbEncrypted;
	}

	public final String getSiteKey() {
		return siteKey;
	}

	protected final void setSiteKey(final String siteKey) {
		this.siteKey = siteKey;
	}

	@Override
	public final String toString() {
		return new ToStringBuilder(this).append("version = " + this.version)
				.append("dbEncrypted = " + this.dbEncrypted).toString();
	}

}
